﻿using Consul;
using LoveEat.API.Read.Applications.Commands.Slaughter;
using LoveEat.API.Read.DTOs;
using LoveEat.Domain.Goods;
using LoveEat.Domain.slaughter;
using LoveEat.ErrorCode;
using LoveEat.Infrastructure.Interfaces;
using MediatR;
using System.Drawing;

namespace LoveEat.API.Read.Applications.CommandHandlers.slaughter
{
    public class slaughterQueryCommandHandler : IRequestHandler<slaughterQueryCommand, APIResult<List<slaughterDTO>>>
    {
        private readonly IBaseRespository<GoodsModel> goodsRep;
        private readonly IBaseRespository<slaughterModel> alauRep;
        private readonly IBaseRespository<CompModel> compRep;

        public slaughterQueryCommandHandler(IBaseRespository<GoodsModel> goodsRep, IBaseRespository<slaughterModel> alauRep, IBaseRespository<CompModel> compRep)
        {
            this.goodsRep = goodsRep;
            this.alauRep = alauRep;
            this.compRep = compRep;
        }

        public Task<APIResult<List<slaughterDTO>>> Handle(slaughterQueryCommand request, CancellationToken cancellationToken)
        {
            APIResult<List<slaughterDTO>> result=new APIResult<List<slaughterDTO>>();
            result.Code=APIResultCode.查询成功;
            result.Message=APIResultCode.查询成功.ToString();

            var res = from a in alauRep.GetAll()
                      join b in compRep.GetAll() on  a.slaughterId equals b.CompId
                      where !b.IsDelete 
                      select new slaughterDTO()
                      {
                          slaughterId=a.slaughterId,
                          Batch=a.Batch,
                          weight=a.weight,
                          CreateTime= a.CreateTime  ,
                          KillTime=a.KillTime,
                          IsDelete= b.IsDelete,
                          CompId=b.CompId,
                          ComName=b.ComName,
                          ComAdds=b.ComAdds,
                          ComTime= b.ComTime,
                          ComInt= b.ComInt,
                      };
            //排序
            res = res.OrderByDescending(x => x.CompId);

            result.Data = res.ToList();
            return Task.FromResult(result);
        }
    }
}
